layui.use(['form','tree','element','table','laytpl','layer','jquery'],function(){
    var form = layui.form,
        element = layui.element;
    $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var table = layui.table;
    var laytpl = layui.laytpl;
    var layedit = layui.layedit;

    var option = {
        elem: '#tableList',
        url : '/log/analysejob/list',
        cellMinWidth : 95,
        page : true,
        height : "full-135",
        limits : [10,15,20,25],
        limit : 10,
        id : "listTable",
        /*toolbar: true,
        totalRow: true,*/
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'jobid', title: '任务编号', minWidth:175, align:"center"},
            {field: 'jobname', title: '任务名称', minWidth:200, align:"center"},
            {field: 'jobtype', title: '处理日志类型', minWidth:175, align:'center',templet:function(d){
                    return d.jobtype;
                }},
            {field: 'businessid', title: '所处业务线', minWidth:200,  align:'center'},
            {field: 'status', title: '状态', align:'center',templet:function(d){
                    return d.status == 0 ? '<span style="color: #ff5722">下线</span>':'<span style="color: #1e9fff;">上线</span>';
                }},
            {field: 'remarks', title: '备注',  align:'center'},
            {field: 'createuser', title: '创建用户', align:'center'},
            {field: 'updateuser', title: '更新用户',  align:'center'},
            {field: 'createdate', minWidth:120, title: '添加时间', sort: true,align:'center',templet:function(d){
                    //处理时间
                    var date = new Date(d.createdate);
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                }},
            {title: '操作', minWidth:225,fixed:"right", templet:'#tableListBar',align:"center"}
        ]]
    };

    //日志分析任务列表
    var tableIns = table.render(option);

    //日志分析任务分类数据
    var height = $("#demo2-view").height();
    $(".orgbox").css('height',height + "px");
    var treeData = {
        elem: '#tree' //指定元素
        , target: '_blank' //是否新选项卡打开（比如节点返回href才有效）
        , click: function (item) { //点击节点回调
            console.log(item);
            option.url = '/log/analysejob/query';
            option.where = {business:item.number};
            table.render(option);
        },
        nodes:null
    };
    //异步加载日志分析任务分类数据
    $.get("/log/analysejob/business/list",function(data) {
        treeData.nodes = data.data;
        layui.tree(treeData);
    });


    //搜索日志分析任务
    $(".search_btn").on("click",function() {
        var searchKey = $(".searchVal").val();
        console.log(searchKey);
        if(searchKey != ''){
            option.url = '';
            option.where = {};
            table.render(option);
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加日志分析任务
    function addUser(edit){
        var index = layui.layer.open({
            title : "添加日志分析任务",
            type : 2,
            content : "analysejob_add.html",
            area: ['100%',$(document).height() + 'px'],
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find("#pid").val(edit.pid);
                    body.find("#pname").val(edit.pname);
                    body.find("#isShow").val(edit.isShow);
                    body.find("#tree-select").val(edit.category);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回日志分析任务列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addBtn").click(function(){
        addUser();
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('listTable'),//此处的id为table数据格式中的id
            data = checkStatus.data,
            idArr = [];
        if(data.length > 0) {
            for (var i in data) {
                idArr.push(data[i].id);
            }
            layer.confirm('确定删除选中的日志分析任务？', {icon: 3, title: '提示信息'}, function (index) {
                for(var i=0;i<idArr.length;i++){
                    $.get("/product/delete?id=" + idArr[i],function(data){
                        if (data.code == 200) {
                            layer.msg("删除成功！");
                        } else {
                            layer.msg("删除失败，请重试！");
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                }
            })
        }else{
            layer.msg("请选择需要删除的日志分析任务");
        }
    });


    //列表操作
    table.on('tool(tableList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit') { //编辑
            var index = layui.layer.open({
                title : "修改日志分析任务信息",
                type : 2,
                content : "analysejob_edit.html?id=" + data.jobid + "&indexName=" + data.jobname,
                area: ['100%',$(document).height() + 'px'],
                success : function(){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回日志分析任务列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            });
        } else if(layEvent === 'edit_rule'){ //配置统计规则
            var index = layui.layer.open({
                title : "配置统计规则信息",
                type : 2,
                content : "analysejobconditionlist.html?jobid=" + data.jobid,
                success : function(){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回日志分析任务列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            });
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此日志分析任务？',{icon:3, title:'提示信息'},function(index){
                $.get("/log/analysejob/delete?id=" + data.jobid + "&indexName=" + data.jobname,function(data){
                    if (data.code == 200) {
                        layer.msg("删除成功！");
                    } else {
                        layer.msg("删除失败，请重试！");
                    }
                    tableIns.reload();
                    layer.close(index);
                });
            });
        }
    });


});